package com.hust360.percent.activity;

import com.hust360.percent.R;
import com.hust360.percent.utils.ActivityUtils;
import com.hust360.percent.utils.DisplayUtils;

import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
	
	/**
	 * TAG for DDMS
	 */
	public static final String TAG = "MainActivity";

	/**
	 * 预先加载类
	 */
	static {
		loadClass();
	}

	/**
	 * 精品tab的索引
	 */
	public static final int BOUTIQUE_TAB_INDEX = 0;
	
	/**
	 * 精品tab的Tag
	 */
	public static final String BOUTIQUE_TAB_TAG = "boutique_tab_tag";
	
	/**
	 * 区域tab的Tag
	 */
	public static final String AREA_TAB_TAG = "area_tab_tag";
	
	/**
	 * 区域tab的索引
	 */
	public static final int AREA_TAB_INDEX = 1;
	
	/**
	 * 收藏tab的Tag
	 */
	public static final String FAVORITE_TAB_TAG = "favorite_tab_tag";
	
	/**
	 * 收藏tab的索引
	 */
	public static final int FAVORITE_TAB_INDEX = 2;

	/**
	 * 整个程序的Tabhost
	 */
	TabHost tabHost;

	/**
	 * 底部的精品的选项卡（通过TextView去实现)
	 */
	Button boutiqueIndicator;
	/**
	 * 位置选项卡
	 */
	Button areaIndicator;
	/**
	 * 我的收藏选项卡
	 */
	Button favoriteIndicator;

	/**
	 * Tab底部的更多按钮
	 */
	Button moreButton;

	/**
	 * tab activity的contentview的root
	 */
	LinearLayout viewRoot;

	/**
	 * Activated Tab
	 */
	int activated_tab;

	/**
	 * last activated tab
	 */
	int last_activated_tab;
	
	/**
	 * 用来添加Tab的Intent
	 */
	Intent intent;
	
	/**
	 * tab底部按钮的监听器
	 */
	TabBtnListener tabBtnListener;
	
	/** */
	TabSpec tabSpec;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityUtils.addActivity(MainActivity.this);
		
		/*
		 * 发送结果信号给欢迎界面的activity，提示它finish掉自己
		 */
		setResult(RESULT_OK);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		init();
		addBoutiqueTab();
		addAreaTab();
		addFavoriteTab();
		calculateStatusBarHeight();
		
		/*
		 * 设置默认激活的tab
		 */
		setDefaultTab(BOUTIQUE_TAB_INDEX);
		activated_tab = BOUTIQUE_TAB_INDEX;
		last_activated_tab = BOUTIQUE_TAB_INDEX;
	}

	private void init() {
		// TODO Auto-generated method stub
		tabHost = getTabHost();
		viewRoot = (LinearLayout) findViewById(R.id.view_root);
		boutiqueIndicator = (Button) findViewById(R.main_id.boutique_btn);
		areaIndicator = (Button) findViewById(R.main_id.area_btn);
		favoriteIndicator = (Button) findViewById(R.main_id.favorite_btn);
		moreButton = (Button) findViewById(R.main_id.more_btn);

		/*
		 * 设置tab底部的监听器
		 */
		tabBtnListener = new TabBtnListener();
		boutiqueIndicator.setOnClickListener(tabBtnListener);
		areaIndicator.setOnClickListener(tabBtnListener);
		favoriteIndicator.setOnClickListener(tabBtnListener);
		moreButton.setOnClickListener(new MoreBtnListener());
	}

	/**
	 * 添加精品选项卡
	 */
	private void addBoutiqueTab() {
		intent = new Intent(this, BoutiqueActivity.class);
		tabSpec = tabHost.newTabSpec(BOUTIQUE_TAB_TAG);
		tabSpec.setIndicator("");
		tabSpec.setContent(intent);
		tabHost.addTab(tabSpec);
	}

	/**
	 * 添加区域选项卡
	 */
	private void addAreaTab() {
		intent = new Intent(this, AreaActivity.class);
		tabSpec = tabHost.newTabSpec(AREA_TAB_TAG);
		tabSpec.setIndicator("");
		tabSpec.setContent(intent);
		tabHost.addTab(tabSpec);
	}

	/**
	 * 添加收藏选项卡
	 */
	private void addFavoriteTab() {
		intent = new Intent(this, FavoriteActivity.class);
		tabSpec = tabHost.newTabSpec(FAVORITE_TAB_TAG);
		tabSpec.setIndicator("");
		tabSpec.setContent(intent);
		tabHost.addTab(tabSpec);
	}
	
	/**
	 * 获取状态栏高度
	 */
	private void calculateStatusBarHeight() {
		tabHost.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Rect rect = new Rect();
				tabHost.getWindowVisibleDisplayFrame(rect);
				DisplayUtils.statusBarHeight = rect.top;
				DisplayUtils.dm = new DisplayMetrics();
				getWindowManager().getDefaultDisplay().getMetrics(
						DisplayUtils.dm);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		ActivityUtils.removeActivity(MainActivity.this);
		super.onDestroy();
	}

	public static void loadClass() {
	}

	private class TabBtnListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			Drawable drawable;
			int color_state_on = getResources().getColor(R.color.textPress);
			int color_state_off = getResources().getColor(R.color.white);
			if (v.getId() == boutiqueIndicator.getId()) {
				activated_tab = BOUTIQUE_TAB_INDEX;
				drawable = getResources().getDrawable(
						R.drawable.boutique_star_on);
				drawable.setBounds(0, 0, drawable.getMinimumWidth(),
						drawable.getMinimumHeight());
				boutiqueIndicator.setCompoundDrawables(null, drawable, null,
						null);
				boutiqueIndicator.setTextColor(color_state_on);
				tabHost.setCurrentTabByTag(BOUTIQUE_TAB_TAG);
			} else if (v.getId() == favoriteIndicator.getId()) {
				activated_tab = FAVORITE_TAB_INDEX;
				drawable = getResources().getDrawable(R.drawable.favorite_on);
				drawable.setBounds(0, 0, drawable.getMinimumWidth(),
						drawable.getMinimumHeight());
				favoriteIndicator.setCompoundDrawables(null, drawable, null,
						null);
				favoriteIndicator.setTextColor(color_state_on);
				tabHost.setCurrentTabByTag(FAVORITE_TAB_TAG);
			} else if (v.getId() == areaIndicator.getId()) {
				activated_tab = AREA_TAB_INDEX;
				drawable = getResources().getDrawable(R.drawable.area_on);
				drawable.setBounds(0, 0, drawable.getMinimumWidth(),
						drawable.getMinimumHeight());
				areaIndicator.setCompoundDrawables(null, drawable, null, null);
				areaIndicator.setTextColor(color_state_on);
				tabHost.setCurrentTabByTag(AREA_TAB_TAG);
			}

			if (activated_tab != last_activated_tab) {
				switch (last_activated_tab) {
				case BOUTIQUE_TAB_INDEX:
					drawable = getResources().getDrawable(
							R.drawable.boutique_star_off);
					drawable.setBounds(0, 0, drawable.getMinimumWidth(),
							drawable.getMinimumHeight());
					boutiqueIndicator.setCompoundDrawables(null, drawable,
							null, null);
					boutiqueIndicator.setTextColor(color_state_off);
					break;
				case AREA_TAB_INDEX:
					drawable = getResources().getDrawable(R.drawable.area_off);
					drawable.setBounds(0, 0, drawable.getMinimumWidth(),
							drawable.getMinimumHeight());
					areaIndicator.setCompoundDrawables(null, drawable, null,
							null);
					areaIndicator.setTextColor(color_state_off);
					break;
				case FAVORITE_TAB_INDEX:
					drawable = getResources().getDrawable(
							R.drawable.favorite_off);
					drawable.setBounds(0, 0, drawable.getMinimumWidth(),
							drawable.getMinimumHeight());
					favoriteIndicator.setCompoundDrawables(null, drawable,
							null, null);
					favoriteIndicator.setTextColor(color_state_off);
					break;
				default:
					Log.e(TAG, "TabBtnListener switch error");
					break;
				}
				last_activated_tab = activated_tab;
			}
		}

	}
	
	private class MoreBtnListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(MainActivity.this, MoreActivity.class);
			startActivity(intent);
		}
		
	}
}
